home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 80 / CD Actual 80 Julio-Agosto 2003.iso / Linux / LinuxGazette / lg / issue72 / misc / nielsen / sql / students.table_functions < prev    next >
Encoding:
Text File  |  2002-08-14  |  21.1 KB  |  650 lines

  1. drop sequence students_sequence;
  2. create sequence students_sequence;
  3. drop sequence students_sequence_backup;
  4. create sequence students_sequence_backup;
  5. drop table students;
  6. create table students (
  7. students_id int4 NOT NULL PRIMARY KEY DEFAULT nextval('students_sequence'),
  8.     date_updated  timestamp NOT NULL default CURRENT_TIMESTAMP,
  9.     date_created  timestamp NOT NULL default CURRENT_TIMESTAMP,
  10.     active int2 CHECK (active in (0,1)) DEFAULT 0,
  11. users_id int4 not null default 0  REFERENCES users
  12.        ON DELETE NO ACTION
  13.        ON UPDATE CASCADE ,
  14. class_id int4 not null default 0  REFERENCES class
  15.        ON DELETE NO ACTION
  16.        ON UPDATE CASCADE 
  17. );drop table students_backup;
  18. create table students_backup (
  19. backup_id int4 NOT NULL UNIQUE DEFAULT nextval('students_sequence_backup'), 
  20.     students_id int4 NOT NULL DEFAULT 0,
  21.     date_updated  timestamp NOT NULL default CURRENT_TIMESTAMP,
  22.     date_created  timestamp NOT NULL default CURRENT_TIMESTAMP,
  23.     active int2 CHECK (active in (0,1)) DEFAULT 0,
  24.     users_id int4 not null default 0,
  25. class_id int4 not null default 0, error_code text NOT NULL DEFAULT ''
  26. );
  27. drop view students_active;
  28. create view students_active as select * from students
  29.         where active = 1;
  30. drop view students_deleted;
  31. create view students_deleted as select * from students
  32.         where active = 0;
  33. drop view students_backup_ids;
  34. create view students_backup_ids as 
  35.            select distinct students_id from students_backup;
  36. drop view students_purged;
  37. create view students_purged as
  38.    select * from students_backup where oid = ANY (
  39.      select max(oid) from students_backup where students_id = ANY
  40.         (
  41.         select distinct students_id from students_backup
  42.           where students_backup.error_code = 'purge'
  43.            and NOT students_id = ANY (select students_id from students)
  44.         )
  45.         group by students_id
  46.      )
  47.     ;
  48. ---              Generic Functions for Perl/Postgresql version 1.0
  49.  
  50. ---                       Copyright 2001, Mark Nielsen
  51. ---                            All rights reserved.
  52. ---    This Copyright notice was copied and modified from the Perl 
  53. ---    Copyright notice. 
  54. ---    This program is free software; you can redistribute it and/or modify
  55. ---    it under the terms of either:
  56.  
  57. ---        a) the GNU General Public License as published by the Free
  58. ---        Software Foundation; either version 1, or (at your option) any
  59. ---        later version, or
  60.  
  61. ---        b) the "Artistic License" which comes with this Kit.
  62.  
  63. ---    This program is distributed in the hope that it will be useful,
  64. ---    but WITHOUT ANY WARRANTY; without even the implied warranty of
  65. ---    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See either
  66. ---    the GNU General Public License or the Artistic License for more details.
  67.  
  68. ---    You should have received a copy of the Artistic License with this
  69. ---    Kit, in the file named "Artistic".  If not, I'll be glad to provide one.
  70.  
  71. ---    You should also have received a copy of the GNU General Public License
  72. ---   along with this program in the file named "Copying". If not, write to the 
  73. ---   Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 
  74. ---    02111-1307, USA or visit their web page on the internet at
  75. ---    http://www.gnu.org/copyleft/gpl.html.
  76.  
  77. -- create a method to unpurge just one item.  
  78. -- create a method to purge one item. 
  79. --  \i /tmp/Test/sample/students.table
  80. ---------------------------------------------------------------------
  81.  
  82. drop function sql_students_insert ();
  83. CREATE FUNCTION sql_students_insert () RETURNS int4 AS '
  84. DECLARE
  85.     record1 record;  oid1 int4; id int4 :=0; record_backup RECORD;
  86. BEGIN
  87.    insert into students (date_updated, date_created, active)
  88.         values (CURRENT_TIMESTAMP,CURRENT_TIMESTAMP, 1);
  89.      -- Get the unique oid of the row just inserted. 
  90.    GET DIAGNOSTICS oid1 = RESULT_OID;
  91.      -- Get the students id. 
  92.    FOR record1 IN SELECT students_id FROM students where oid = oid1
  93.       LOOP
  94.       id := record1.students_id;
  95.    END LOOP;
  96.    
  97.      -- If id is NULL, insert failed or something is wrong.
  98.    IF id is NULL THEN return (-1); END IF;
  99.      -- It should also be greater than 0, otherwise something is wrong.
  100.    IF id < 1 THEN return (-2); END IF;
  101.  
  102.       -- Now backup the data. 
  103.     FOR record_backup IN SELECT * FROM students where students_id = id
  104.        LOOP
  105.        insert into students_backup (students_id, date_updated, date_created, 
  106.            active, error_code) 
  107.          values (id, record_backup.date_updated, record_backup.date_created,
  108.             record_backup.active, ''insert'');
  109.     END LOOP;
  110.  
  111.      -- Everything has passed, return id as students_id.
  112.    return (id);
  113. END;
  114. ' LANGUAGE 'plpgsql';
  115. ---------------------------------------------------------------------
  116.  
  117. drop function sql_students_delete (int4);
  118. CREATE FUNCTION sql_students_delete (int4) RETURNS int2 AS '
  119. DECLARE
  120.     id int4 := 0;
  121.     id_exists int4 := 0;
  122.     record1 RECORD; 
  123.     record_backup RECORD;
  124.     return_int4 int4 :=0;
  125.  
  126. BEGIN
  127.      -- If the id is not greater than 0, return error.
  128.    id := clean_numeric($1);
  129.    IF id < 1 THEN return -1; END IF;
  130.  
  131.      -- If we find the id, set active = 0. 
  132.    FOR record1 IN SELECT students_id FROM students 
  133.           where students_id = id
  134.       LOOP
  135.       update students set active=0, date_updated = CURRENT_TIMESTAMP
  136.            where students_id = id;  
  137.       GET DIAGNOSTICS return_int4 = ROW_COUNT;       
  138.       id_exists := 1;
  139.    END LOOP;
  140.       
  141.      -- If we did not find the id, abort and return -2.  
  142.    IF id_exists = 0 THEN return (-2); END IF;
  143.  
  144.    FOR record_backup IN SELECT * FROM students where students_id = id
  145.       LOOP
  146.       insert into students_backup (students_id, date_updated, date_created,
  147.           active , users_id, class_id ,error_code)
  148.            values (record_backup.students_id, record_backup.date_updated, 
  149.              record_backup.date_updated, record_backup.active
  150.              , record_backup.users_id, record_backup.class_id , ''delete''
  151.       );
  152.    END LOOP;
  153.  
  154.      -- If id_exists == 0, Return error.
  155.      -- It means it never existed. 
  156.    IF id_exists = 0 THEN return (-1); END IF;
  157.  
  158.      -- We got this far, it must be true, return ROW_COUNT.   
  159.    return (return_int4);
  160. END;
  161. ' LANGUAGE 'plpgsql';
  162.  
  163. ---------------------------------------------------------------------
  164.  
  165. drop function sql_students_undelete (int4);
  166. CREATE FUNCTION sql_students_undelete (int4) RETURNS int2 AS '
  167. DECLARE
  168.     id int4 := 0;
  169.     id_exists int4 := 0;
  170.     record1 RECORD; 
  171.     record_backup RECORD;
  172.     return_int4 int4 :=0;
  173.  
  174. BEGIN
  175.      -- If the id is not greater than 0, return error.
  176.    id := clean_numeric($1);
  177.    IF id < 1 THEN return -1; END IF;
  178.  
  179.      -- If we find the id, set active = 1. 
  180.    FOR record1 IN SELECT students_id FROM students 
  181.           where students_id = id
  182.       LOOP
  183.       update students set active=1, date_updated = CURRENT_TIMESTAMP
  184.            where students_id = id;  
  185.       GET DIAGNOSTICS return_int4 = ROW_COUNT;       
  186.       id_exists := 1;
  187.    END LOOP;
  188.       
  189.      -- If we did not find the id, abort and return -2.  
  190.    IF id_exists = 0 THEN return (-2); END IF;
  191.  
  192.    FOR record_backup IN SELECT * FROM students where students_id = id
  193.       LOOP
  194.       insert into students_backup (students_id, date_updated, date_created,
  195.           active , users_id, class_id ,error_code)
  196.            values (record_backup.students_id, record_backup.date_updated, 
  197.              record_backup.date_updated, record_backup.active
  198.              , record_backup.users_id, record_backup.class_id , ''undelete''
  199.       );
  200.    END LOOP;
  201.  
  202.      -- If id_exists == 0, Return error.
  203.      -- It means it never existed. 
  204.    IF id_exists = 0 THEN return (-1); END IF;
  205.  
  206.      -- We got this far, it must be true, return ROW_COUNT.   
  207.    return (return_int4);
  208. END;
  209. ' LANGUAGE 'plpgsql';
  210.  
  211. ---------------------------------------------------------------------
  212. drop function sql_students_update (int4 , int4, int4);
  213. CREATE FUNCTION sql_students_update  (int4 , int4, int4) 
  214.   RETURNS int2 AS '
  215. DECLARE
  216.     id int4 := 0;
  217.     id_exists int4 := 0;
  218.     record_update RECORD; record_backup RECORD;
  219.     return_int4 int4 :=0;
  220.               var_2 int4;
  221.           var_3 int4;
  222.  
  223. BEGIN
  224.              var_2 := clean_numeric($2);
  225.          var_3 := clean_numeric($3);
  226.  
  227.      -- If the id is not greater than 0, return error.
  228.    id := clean_numeric($1);
  229.    IF id < 1 THEN return -1; END IF;
  230.  
  231.    FOR record_update IN SELECT students_id FROM students
  232.          where students_id = id
  233.       LOOP
  234.       id_exists := 1;
  235.    END LOOP;
  236.  
  237.    IF id_exists = 0 THEN return (-2); END IF;
  238.  
  239.    update students set date_updated = CURRENT_TIMESTAMP
  240.       , users_id = var_2, class_id = var_3 
  241.         where students_id = id;
  242.    GET DIAGNOSTICS return_int4 = ROW_COUNT;
  243.  
  244.    FOR record_backup IN SELECT * FROM students where students_id = id
  245.       LOOP
  246.      insert into students_backup (students_id,
  247.          date_updated, date_created, active
  248.          , users_id, class_id, error_code)
  249.        values (record_update.students_id, record_backup.date_updated,
  250.          record_backup.date_updated, record_backup.active
  251.          , record_backup.users_id, record_backup.class_id, ''update''
  252.       );
  253.    END LOOP;
  254.  
  255.      -- We got this far, it must be true, return ROW_COUNT.   
  256.    return (return_int4);
  257. END;
  258. ' LANGUAGE 'plpgsql';
  259. ---------------------------------------------------------------------
  260.  
  261. drop function sql_students_copy (int4);
  262. CREATE FUNCTION sql_students_copy (int4) 
  263.   RETURNS int2 AS '
  264. DECLARE
  265.     id int4 := 0;
  266.     id_exists int4 := 0;
  267.     record1 RECORD; record2 RECORD; record3 RECORD;    
  268.     return_int4 int4 := 0;
  269.     id_new int4 := 0;
  270.     students_new int4 :=0;
  271. BEGIN
  272.      -- If the id is not greater than 0, return error.
  273.    id := clean_numeric($1);
  274.    IF id < 1 THEN return -1; END IF;
  275.  
  276.    FOR record1 IN SELECT students_id FROM students where students_id = id
  277.       LOOP
  278.       id_exists := 1;
  279.    END LOOP;
  280.    IF id_exists = 0 THEN return (-2); END IF;
  281.  
  282.      --- Get the new id
  283.    FOR record1 IN SELECT sql_students_insert() as students_insert
  284.       LOOP
  285.       students_new := record1.students_insert;
  286.    END LOOP;
  287.      -- If the students_new is not greater than 0, return error.
  288.    IF students_new < 1 THEN return -3; END IF;
  289.  
  290.    FOR record2 IN SELECT * FROM students where students_id = id
  291.       LOOP
  292.  
  293.      FOR record1 IN SELECT sql_students_update(students_new , clean_text(record2.users_id), clean_text(record2.class_id))
  294.         as students_insert
  295.       LOOP
  296.         -- execute some arbitrary command just to get it to pass. 
  297.       id_exists := 1;
  298.      END LOOP;
  299.    END LOOP;
  300.  
  301.      -- We got this far, it must be true, return new id.   
  302.    return (students_new);
  303. END;
  304. ' LANGUAGE 'plpgsql';
  305.  
  306. ------------------------------------------------------------------
  307. drop function sql_students_purge ();
  308. CREATE FUNCTION sql_students_purge () RETURNS int4 AS '
  309. DECLARE
  310.     record_backup RECORD; oid1 int4 := 0;
  311.     return_int4 int4 :=0;
  312.     deleted int4 := 0;
  313.     delete_count int4 :=0;
  314.     delete_id int4;
  315.  
  316. BEGIN 
  317.  
  318.      -- Now delete one by one. 
  319.    FOR record_backup IN SELECT * FROM students where active = 0
  320.       LOOP
  321.          -- Record the id we want to delete. 
  322.       delete_id = record_backup.students_id;
  323.  
  324.       insert into students_backup (students_id, date_updated, date_created,
  325.           active , users_id, class_id ,error_code)
  326.            values (record_backup.students_id, record_backup.date_updated, 
  327.              record_backup.date_updated, record_backup.active
  328.              , record_backup.users_id, record_backup.class_id , ''purge''
  329.           );
  330.  
  331.         -- Get the unique oid of the row just inserted. 
  332.       GET DIAGNOSTICS oid1 = RESULT_OID;
  333.  
  334.         -- If oid1 less than 1, return -1
  335.       IF oid1 < 1 THEN return (-2); END IF;
  336.         -- Now delete this from the main table.   
  337.       delete from students where students_id = delete_id;
  338.  
  339.         -- Get row count of row just deleted, should be 1. 
  340.       GET DIAGNOSTICS deleted = ROW_COUNT;
  341.         -- If deleted less than 1, return -3
  342.       IF deleted < 1 THEN return (-3); END IF;
  343.       delete_count := delete_count + 1;
  344.  
  345.     END LOOP;
  346.  
  347.      -- We got this far, it must be true, return the number of ones we had.  
  348.    return (delete_count);
  349. END;
  350. ' LANGUAGE 'plpgsql';
  351.  
  352. ------------------------------------------------------------------
  353. drop function sql_students_purgeone (int4);
  354. CREATE FUNCTION sql_students_purgeone (int4) RETURNS int4 AS '
  355. DECLARE
  356.     record_backup RECORD; oid1 int4 := 0;
  357.     record1 RECORD;
  358.     return_int4 int4 :=0;
  359.     deleted int4 := 0;
  360.     delete_count int4 :=0;
  361.     delete_id int4;
  362.     purged_no int4 := 0;
  363.  
  364. BEGIN
  365.  
  366.     delete_id := $1;
  367.         -- If purged_id less than 1, return -4
  368.     IF delete_id < 1 THEN return (-4); END IF;
  369.  
  370.    FOR record1 IN SELECT * FROM students 
  371.       where active = 0 and students_id = delete_id 
  372.       LOOP
  373.       purged_no := purged_no + 1;
  374.    END LOOP;
  375.  
  376.         -- If purged_no less than 1, return -1
  377.    IF purged_no < 1 THEN return (-1); END IF;
  378.  
  379.      -- Now delete one by one.
  380.    FOR record_backup IN SELECT * FROM students where students_id = delete_id
  381.       LOOP
  382.  
  383.       insert into students_backup (students_id, date_updated, date_created,
  384.           active , users_id, class_id ,error_code)
  385.            values (record_backup.students_id, record_backup.date_updated,
  386.              record_backup.date_updated, record_backup.active
  387.              , record_backup.users_id, record_backup.class_id , ''purgeone''
  388.           );
  389.  
  390.         -- Get the unique oid of the row just inserted.
  391.       GET DIAGNOSTICS oid1 = RESULT_OID;
  392.  
  393.         -- If oid1 less than 1, return -2
  394.       IF oid1 < 1 THEN return (-2); END IF;
  395.         -- Now delete this from the main table.
  396.       delete from students where students_id = delete_id;
  397.  
  398.         -- Get row count of row just deleted, should be 1.
  399.       GET DIAGNOSTICS deleted = ROW_COUNT;
  400.         -- If deleted less than 1, return -3
  401.       IF deleted < 1 THEN return (-3); END IF;
  402.       delete_count := delete_count + 1;
  403.  
  404.     END LOOP;
  405.  
  406.      -- We got this far, it must be true, return the number of ones we had.
  407.    return (delete_count);
  408. END;
  409. ' LANGUAGE 'plpgsql';
  410.  
  411. ------------------------------------------------------------------------
  412. drop function sql_students_unpurge ();
  413. CREATE FUNCTION sql_students_unpurge () RETURNS int2 AS '
  414. DECLARE
  415.     record1 RECORD;
  416.     record2 RECORD; 
  417.     record_backup RECORD;
  418.     purged_id int4 := 0;
  419.     purge_count int4 :=0;
  420.     timestamp1 timestamp;
  421.     purged_no int4 := 0;
  422.     oid1 int4 := 0;
  423.     oid_found int4 := 0;
  424.     highest_oid int4 := 0;
  425.  
  426. BEGIN
  427.  
  428.      -- Now get the unique ids that were purged. 
  429.    FOR record1 IN select distinct students_id from students_backup 
  430.        where students_backup.error_code = ''purge''
  431.           and NOT students_id = ANY (select students_id from students)
  432.       LOOP
  433.  
  434.       purged_id := record1.students_id;
  435.       timestamp1 := CURRENT_TIMESTAMP;
  436.       purged_no := purged_no + 1;
  437.       oid_found := 0;
  438.       highest_oid := 0;
  439.  
  440.         -- Now we have the unique id, find its latest date. 
  441.  
  442.       FOR record2 IN select max(oid) from students_backup 
  443.           where students_id = purged_id and error_code = ''purge''
  444.         LOOP 
  445.           -- record we got the date and also record the highest date.
  446.         oid_found := 1; 
  447.         highest_oid := record2.max;
  448.       END LOOP;
  449.  
  450.          -- If the oid_found is 0, return error. 
  451.       IF oid_found = 0 THEN return (-3); END IF;
  452.  
  453.         -- Now we have the latest date, get the values and insert them. 
  454.       FOR record_backup IN select * from students_backup 
  455.           where oid = highest_oid
  456.         LOOP 
  457.  
  458.       insert into students_backup (students_id, date_updated, date_created,
  459.           active , users_id, class_id ,error_code)
  460.            values (purged_id, record_backup.date_updated, 
  461.              timestamp1, record_backup.active
  462.              , record_backup.users_id, record_backup.class_id , ''unpurge''
  463.           );
  464.  
  465.         -- Get the unique oid of the row just inserted. 
  466.       GET DIAGNOSTICS oid1 = RESULT_OID;
  467.         -- If oid1 less than 1, return -1
  468.       IF oid1 < 1 THEN return (-1); END IF;
  469.  
  470.       insert into students (students_id, date_updated, date_created,
  471.           active , users_id, class_id)
  472.            values (purged_id, timestamp1,
  473.              timestamp1, record_backup.active
  474.              , record_backup.users_id, record_backup.class_id );
  475.         -- Get the unique oid of the row just inserted.
  476.       GET DIAGNOSTICS oid1 = RESULT_OID;
  477.         -- If oid1 less than 1, return -2
  478.       IF oid1 < 1 THEN return (-2); END IF;
  479.  
  480.       END LOOP;
  481.  
  482.    END LOOP;
  483.  
  484.      -- We got this far, it must be true, return how many were affected.  
  485.    return (purged_no);
  486. END;
  487. ' LANGUAGE 'plpgsql';
  488.  
  489. ---------------------------------------------------------------------
  490. drop function sql_students_unpurgeone (int4);
  491. CREATE FUNCTION sql_students_unpurgeone (int4) RETURNS int2 AS '
  492. DECLARE
  493.     record_id int4;
  494.     record1 RECORD;
  495.     record2 RECORD;
  496.     record_backup RECORD;
  497.     return_int4 int4 :=0;
  498.     purged_id int4 := 0;
  499.     purge_count int4 :=0;
  500.     timestamp1 timestamp;
  501.     purged_no int4 := 0;
  502.     oid1 int4 := 0;
  503.     oid_found int4 := 0;
  504.     highest_oid int4 := 0;
  505.  
  506. BEGIN
  507.  
  508.       purged_id := $1;
  509.         -- If purged_id less than 1, return -1
  510.       IF purged_id < 1 THEN return (-1); END IF;
  511.         --- Get the current timestamp.
  512.       timestamp1 := CURRENT_TIMESTAMP;
  513.  
  514.    FOR record1 IN select distinct students_id from students_backup
  515.        where students_backup.error_code = ''purge''
  516.           and NOT students_id = ANY (select students_id from students)
  517.           and students_id = purged_id
  518.       LOOP
  519.       purged_no := purged_no + 1;
  520.  
  521.    END LOOP;
  522.  
  523.         -- If purged_no less than 1, return -1
  524.    IF purged_no < 1 THEN return (-3); END IF;
  525.  
  526.         -- Now find the highest oid.  
  527.    FOR record2 IN select max(oid) from students_backup
  528.           where students_id = purged_id and error_code = ''purge''
  529.         LOOP
  530.           -- record we got the date and also record the highest date.
  531.         oid_found := 1;
  532.         highest_oid := record2.max;
  533.     END LOOP;
  534.  
  535.          -- If the oid_found is 0, return error.
  536.     IF oid_found = 0 THEN return (-4); END IF;
  537.  
  538.         -- Now get the data and restore it. 
  539.     FOR record_backup IN select * from students_backup 
  540.           where oid  = highest_oid
  541.         LOOP 
  542.         -- Insert into backup that it was unpurged. 
  543.       insert into students_backup (students_id, date_updated, date_created,
  544.           active , users_id, class_id ,error_code)
  545.            values (purged_id, timestamp1, 
  546.              record_backup.date_created, record_backup.active
  547.              , record_backup.users_id, record_backup.class_id , ''unpurgeone''
  548.           );
  549.  
  550.         -- Get the unique oid of the row just inserted. 
  551.       GET DIAGNOSTICS oid1 = RESULT_OID;
  552.         -- If oid1 less than 1, return -1
  553.       IF oid1 < 1 THEN return (-1); END IF;
  554.         -- Insert into live table. 
  555.       insert into students (students_id, date_updated, date_created,
  556.           active , users_id, class_id)
  557.            values (record_backup.students_id, timestamp1,
  558.              record_backup.date_updated, record_backup.active
  559.              , record_backup.users_id, record_backup.class_id );
  560.         -- Get the unique oid of the row just inserted.
  561.       GET DIAGNOSTICS oid1 = RESULT_OID;
  562.         -- If oid1 less than 1, return -2
  563.       IF oid1 < 1 THEN return (-2); END IF;
  564.  
  565.       END LOOP;
  566.  
  567.      -- We got this far, it must be true, return how many were affected (1).  
  568.    return (purged_no);
  569. END;
  570. ' LANGUAGE 'plpgsql';
  571.  
  572. insert into students (students_id, date_updated, date_created, active)
  573.     values (0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 0);
  574. insert into students_backup (backup_id, students_id, 
  575.      date_updated, date_created, active, error_code)
  576.     values (0, 0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 0, 'table creation');
  577.  
  578.  
  579.  
  580.  
  581. drop function clean_text (text);
  582. CREATE FUNCTION  clean_text (text) RETURNS text AS '
  583.   my $Text = shift;
  584.     # Get rid of whitespace in front. 
  585.   $Text =~ s/^\\s+//;
  586.     # Get rid of whitespace at end. 
  587.   $Text =~ s/\\s+$//;
  588.     # Get rid of anything not text.
  589.   $Text =~ s/[^ a-z0-9\\/\\`\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\-\\_\\=\\+\\\\\\|\[\\{\\]\\}\\;\\:\\''\\"\\,\\<\\.\\>\\?\\t\\n]//gi;
  590.     # Replace all multiple whitespace with one space. 
  591.   $Text =~ s/\\s+/ /g;
  592.   return $Text;
  593. ' LANGUAGE 'plperl';
  594.  -- Just do show you what this function cleans up. 
  595. select clean_text ('       ,./<>?aaa aa      !@#$%^&*()_+| ');
  596.  
  597. drop function clean_alpha (text);
  598. CREATE FUNCTION  clean_alpha (text) RETURNS text AS '
  599.   my $Text = shift;
  600.   $Text =~ s/[^a-z0-9_]//gi;
  601.   return $Text;
  602. ' LANGUAGE 'plperl';
  603.  -- Just do show you what this function cleans up. 
  604. select clean_alpha ('       ,./<>?aaa aa      !@#$%^&*()_+| ');
  605.  
  606. drop function clean_numeric (text);
  607. CREATE FUNCTION  clean_numeric (text) RETURNS int4 AS '
  608.   my $Text = shift;
  609.   $Text =~ s/[^0-9]//gi;
  610.   return $Text;
  611. ' LANGUAGE 'plperl';
  612.  -- Just do show you what this function cleans up.
  613. select clean_numeric ('       ,./<>?aaa aa      !@#$%^&*()_+| ');
  614.  
  615. drop function clean_numeric (int4);
  616. CREATE FUNCTION  clean_numeric (int4) RETURNS int4 AS '
  617.   my $Text = shift;
  618.   $Text =~ s/[^0-9]//gi;
  619.   return $Text;
  620. ' LANGUAGE 'plperl';
  621.  -- Just do show you what this function cleans up.
  622. select clean_numeric (11111);
  623.  
  624.  
  625.  
  626. select sql_students_insert();
  627. select sql_students_update(1,1,1);
  628.  
  629. select sql_students_insert();
  630. select sql_students_update(2,2,1);
  631.  
  632. select sql_students_insert();
  633. select sql_students_update(3,1,2);
  634.  
  635. select sql_students_insert();
  636. select sql_students_update(4,2,2);
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649. vacuum;
  650.